package main

import (
	item "example_shop/kitex_gen/example/shop/item/itemservice"
	"log"
	"net"

	"github.com/cloudwego/kitex/pkg/rpcinfo"
	"github.com/cloudwego/kitex/server"
	etcd "github.com/kitex-contrib/registry-etcd"
)

func main() {
	// svr := item.NewServer(new(ItemServiceImpl))

	// 使用时请传入真实 etcd 的服务地址，本例中为 127.0.0.1:2379
	r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:3379"})
	if err != nil {
		log.Fatal(err)
	}

	itemServiceImpl := new(ItemServiceImpl)
	// stockCli, err := stockservice.NewClient("example.shop.stock", client.WithHostPorts("0.0.0.0:8890"))
	stockCli, err := NewStockClient()
	if err != nil {
		log.Fatal("Failed to create stock client:", err)
	}

	itemServiceImpl.stockCli = stockCli

	addr, _ := net.ResolveTCPAddr("tcp", ":8888")
	svr := item.NewServer(itemServiceImpl,
		server.WithServiceAddr(addr),
		server.WithRegistry(r),
		server.WithServerBasicInfo(
			&rpcinfo.EndpointBasicInfo{
				ServiceName: "example.shop.item",
			},
		),
	)
	err = svr.Run()

	if err != nil {
		log.Println(err.Error())
	}
}
